﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Mvc.Routes;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System;
using Ascent.XinKuanKong.Services;
using Orchard;
using Orchard.Logging;
using Orchard.FileSystems.AppData;
using System.Web.Http;
using System.Web.Http.WebHost.Routing;
using System.Web.Http.WebHost;

namespace Ascent.XinKuanKong
{
    public class Routes : IRouteProvider
    {
        private readonly IOrchardServices _orchardServices;
        private readonly IAppDataFolder _appDataFolder;
        public ILogger Logger { get; set; }
        public Routes(IOrchardServices orchardServices, IAppDataFolder appDataFolder)
        {
            _orchardServices = orchardServices;
            _appDataFolder = appDataFolder;
            Logger = NullLogger.Instance;
            Logger.Information(_appDataFolder.GetVirtualPath("~/"));
        }

        public void GetRoutes(ICollection<RouteDescriptor> routes)
        {
            foreach (var routeDescriptor in GetRoutes())
                routes.Add(routeDescriptor);
        }

        public IEnumerable<RouteDescriptor> GetRoutes()
        {
            return new[] {                             
                new RouteDescriptor {
                    Route = new Route(
                        "xinkuankong",
                        new RouteValueDictionary {
                            {"area", "Ascent.XinKuanKong"},
                            {"controller", "Home"},
                            {"action", "Index"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "Ascent.XinKuanKong"}
                        },
                        new MvcRouteHandler())
                },
                new RouteDescriptor {
                    Route = new HttpWebRoute ("xinkuankong/api/{controller}/{id}", HttpControllerRouteHandler.Instance)
                }//,
                //new RouteDescriptor{
                //    Route = RouteTable.Routes.MapHttpRoute(
                //        name: "DefaultApi",
                //        routeTemplate: "xinkuankong/api/{controller}/{id}",
                //        defaults: new { id = RouteParameter.Optional }
                //)}//,
                //new RouteDescriptor 
                //{
                //    Route = new DynamicServiceRoute(
                //        "xinkuankong/odata",
                //        null,
                //        //new OrchardServiceHostFactory(),
                //        new DataServiceHostFactory(),
                //        typeof(TopDataService))
                //}
            };
        }
    }
}